package org.zju.demo.service.impl;

import org.zju.demo.model.Order;
import org.zju.demo.service.intf.OrderInfoIntf;
import org.zju.demo.utils.Constant;
import org.zju.demo.utils.HibernateUtil;

import java.util.List;

/**
 * Created by admin on 2015/8/27.
 */
public class OrderInfoImpl implements OrderInfoIntf {
    public Order findById(int Id) {
        String hql = "from Order where orderId=?";
        String[] parameters = {Integer.toString(Id)};
        return (Order) HibernateUtil.objectQuery(hql, parameters);
    }

    public int addOrder(Order order) {
        return HibernateUtil.saveIfNotExists(order);
    }

    public int update(Order order) {
        Order obj = findById(order.getOrderId());
        if (obj == null) {
            return Constant.UNKNOWN_ORDER;
        }
        HibernateUtil.update(order);
        return Constant.SUCCESS;
    }

    public Order findByTimeAndUser(Order order) {
        String hql = "from Order where time=? and customName=?";
        String[] parameters = {order.getTime().toString(), order.getCustomName()};
        return (Order) HibernateUtil.objectQuery(hql, parameters);
    }

    public List<Order> getAllOrders() {
        String hql = "from Order";
        return HibernateUtil.executeQuery(hql, null);
    }

    public List<Order> getSpecificOrders(String status) {
        String hql = "from Order where status=?";
        String[] parameters = {status};
        return HibernateUtil.executeQuery(hql, parameters);
    }
}
